#!/bin/bash

# Bridge interface example.
# With the following physical topology:

# +--------+       +------------+
# | SERVER |       | CONTROLLER |
# +--------+       +------------+
#   eth1 (ns1) ------> port1
#   eth2 (ns2) ------> port2

BR_VLAN_RANGE_DEF="{4..6}"
BR_VLAN_RANGE=${BR_VLAN_RANGE:-$BR_VLAN_RANGE_DEF}
TENANT_PREFIX=${TENANT_PREFIX:-"10.0"} # followed by VID.HOSTID

PORT=${PORT:-eth}
INTF=($PORT1 $PORT2)

CFG=${CFG:-"a"}

function nsexec {
  ip netns exec ns${NS} $@
}

function nswipe {
  # remove ns
  ip netns delete ns${NS}
}

# setup
function setup {
  for id in {0..1}; do

    export NS=$id

    # configure namespace
    ip netns add ns$id

    # specific setup
    ip link set ${PORT}$id netns ns$id

    # bring interfaces in namespace up
    nsexec ip link set lo up
    nsexec ip link set ${PORT}$id up

    #configure VLANs and add IP address
    for vid in $(eval echo $BR_VLAN_RANGE) ; do
      nsexec ip l add link ${PORT}$id name ${PORT}$id.$vid type vlan id $vid
      nsexec ip a add ${TENANT_PREFIX}.${vid}.2${id}/24 dev ${PORT}$id.$vid
      nsexec ip l set ${PORT}$id.$vid up
    done
 done
}

setup
